package com.fiume.travel.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * Description: 跨域访问拦截器
 * </p>
 *
 * @author fiume
 * @version v1.0.0
 * @create 2021-03-02 17:38
 * @see com.fiume.travel.interceptor
 */
public class CrossInterceptor implements HandlerInterceptor {

    /**
     * 在处理器执行前进行拦截
     * 放行跨域访问
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        //解决跨域问题
        // 获取请求的起源,本次请求来自哪个页面
        String origin = request.getHeader("Origin");
        // 对所有的跨域资源都放行,运行跨域访问
        response.setHeader("Access-Control-Allow-Origin", origin);
        // 对所有的方法都允许
        response.setHeader("access-control-allow-methods", "*");
        //是否支持cookie跨域
        response.setHeader("access-control-allow-credentials", "true");
        //指定头部跨域传递accessToken
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");

        return true;
    }

}
